package com.example.demo.producer.impl;

import java.time.LocalDate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import com.example.demo.producer.SyncProducer;
import com.example.demo.producer.WeekEnum;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class SyncProducerImpl implements SyncProducer{

	@Autowired
    JavaMailSender javaMailSender;
	
	
	public static void main(String[] args) throws InterruptedException {
		LocalDate nowDate = LocalDate.of(2020, 11, 15);
		while(true) {
			Thread.sleep(400);
			nowDate = nowDate.plusDays(4);
			String dayOfWeek = String.valueOf(nowDate.getDayOfWeek());
			System.out.println(nowDate + ":" + WeekEnum.getDesByCode(dayOfWeek));
		}
	}
	
	@Override
	public void sysBeDuty() throws InterruptedException {
		LocalDate nowDate = LocalDate.of(2020, 11, 15);
		while(nowDate.isBefore(LocalDate.now())) {
			LocalDate nextDay = nowDate.plusDays(4);
			String dayOfWeek = String.valueOf(nextDay.getDayOfWeek());
			System.out.println(nextDay + ":" + WeekEnum.getDesByCode(dayOfWeek));
			if(nextDay.equals(LocalDate.now())) {
				log.info("发送邮件！");
				sendMsg();
			}
			nowDate = nextDay;
		}
	}
	
	@Override
	public void sendMsg() {
		SimpleMailMessage message = new SimpleMailMessage();
        message.setTo("1170001884@qq.com");
        message.setFrom("657786624@qq.com");
        message.setSubject("吃饭了！");
        message.setText("干饭了！干饭了！");
        javaMailSender.send(message);
	}
}
